﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BrnMall.Manager.Domain.User.User;

namespace BrnMall.Manager.Manager.User
{
    public static class UsersManager
    {

        public static List<int> GetIdsByNickName(string nickName, int pageSize)
        {
            try
            {
                using (var db = new QDMallDatabase())
                {
                    return db.Fetch<int>(string.Format("select top {0} uid from bma_users where nickname=@0", pageSize), nickName);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }

        public static void UpdateUserIMState(int uid, bool online)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update bma_users set imonline=@0 where uid=@1", online ? 1 : 0, uid);
            }
        }

        public static bool CheckIMOnline(int uid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.ExecuteScalar<bool>("select IMOnline from bma_users where uid=@0", uid);
            }
        }

        public static string GetIdsByNickNameToString(string nickName, int pageSize)
        {
            var list = GetIdsByNickName(nickName, pageSize);
            StringBuilder sb = new StringBuilder();
            sb.Append("0");
            list.ForEach(x => sb.AppendFormat(",{0}", x));

            return sb.ToString();
        }
        public static List<UsersModel> GetByIds(string uid)
        {
            var sql = string.Empty;
            if (uid.Contains(","))
            {
                var uids = (uid.Substring(0, uid.Length - 1));
                sql = string.Format("select uid,username,email,storeid,nickname,avatar from bma_users where uid in({0})", uids);
            }   
            else
                sql = string.Format("select uid,username,email,storeid,nickname,avatar from bma_users where uid={0}", uid);

            using (var db = new QDMallDatabase())
            {
                return db.Fetch<UsersModel>(sql);
            }
        }

        /// <summary>
        /// 从会员列表中搜索指定ID，其中LOGO是完整路径
        /// </summary>
        public static UsersModel FindUser(List<UsersModel> list, int uid)
        {
            if (list != null)
            {
                foreach (var item in list)
                {
                    if (item.Uid == uid)
                    {
                        item.Avatar = FormatLogo(item.Avatar, item.StoreId);
                        return item;
                    }
                }
            }
            return new UsersModel();
        }

        /// <summary>
        /// 返回完整路径的logo
        /// </summary>
        /// <param name="logo"></param>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public static string FormatLogo(string logo, int storeId)
        {
            if (logo.Length > 0 && logo.IndexOf("http") != 0)
                return string.Format("http://static.711688.net.cn/upload/store/{0}/logo/source/{1}", storeId, logo);
            return logo;
        }

        public static UsersModel Get(int uid)
        {
            var sql = string.Format("select uid,nickname,avatar,mobile from bma_users where uid={0}", uid);
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<UsersModel>(sql);
            }
        }

    }
}
